package isae.c2.android.depense;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;
import isae.c2.android.depense.DB.CategoryDBAdapter;

public class list_category extends ListActivity {
    private CategoryDBAdapter dbHelper;
    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;
    private static final int DELETE_ID = Menu.FIRST + 1;
    private Cursor cursor;

    

/** Cette methode est appele quand l'activite est cree.
Dans cette methode on specfie le layout file qui est de type xml  et on ouvre la connexion a la base de donnee
 */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_category);
        this.getListView().setDividerHeight(2);
        dbHelper = new CategoryDBAdapter(this);
        dbHelper.open();
        fillData();
        registerForContextMenu(getListView());
    }


    // Creer le menu basee sur la definition du fichier XML
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.listmenu, menu);
        return true;
    }


    // Reaction quand le menu est selecionne
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
        case R.id.insert:
            createTodo();
            return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.insert:
            createTodo();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                    .getMenuInfo();
            dbHelper.deleteTodo(info.id);
            fillData();
            return true;
        }
        return super.onContextItemSelected(item);
    }

 
   // La methode qui cree la categorie

    private void createTodo() {
        Intent i = new Intent(this, create_category.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }


    // ListView et view (row) , position et id de la menu selectionnee

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, create_category.class);
        i.putExtra(CategoryDBAdapter.KEY_ROWID, id);
        // Activity returns an result if called with startActivityForResult
        
        startActivityForResult(i, ACTIVITY_EDIT);
    }

 
    //Cette methode est appele avec le resultat de l'autre activite
    //L'argument "requestCode" est l'original  code qui est envoye a l'activite
    //L'argument "resultCode" est  le return code , 0 si tout est ok
    //L'argument "intent" peut etre utiliser pour prendre des donees du Caller
    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();

    }
  
   //cette methode remplis les donnees de la categorie quand on modifie la categorie

    private void fillData() {
        cursor = dbHelper.fetchAllTodos();
        startManagingCursor(cursor);

        String[] from = new String[] { CategoryDBAdapter.KEY_CODE };
        int[] to = new int[] { R.id.label };


        //Now create an array adapter and set it to display using our row

        SimpleCursorAdapter notes = new SimpleCursorAdapter(this,

                R.layout.todo_row, cursor, from, to);
        setListAdapter(notes);
    }

 
    //La methode qui supprime la categorie 

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbHelper != null) {
            dbHelper.close();
        }
    }
}